package com.yonyou.crm.sys.modules.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.sys.modules.entity.ModuleVO;
import com.yonyou.iuap.context.InvocationInfoProxy;

@Service
public class ModuleBizServiceImpl extends BaseBizServiceImpl<ModuleVO>{	
	/**
	 * 获取对象列表
	 * 
	 * @return
	 */
	public List<ModuleVO> getModuleList(){
		String tenantIdStr = (String)InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("tenantId", tenantId);

		List<ModuleVO> moduleList = new ArrayList<ModuleVO>();
		moduleList = dao.selectVoList(ModuleVO.class, "ListModule", paramMap);
		return moduleList;
	}
	
	public Object selectFieldsByIds(Object[] ids) {
		List<Map<String, Object>> rs = new ArrayList<Map<String,Object>>();
		List <ModuleVO> moduleVOs =getModuleList();
		for (ModuleVO moduleVO:moduleVOs) {
				for(Object obj: ids) {
					if (moduleVO.getId().equals((Long)obj)) {
						Map<String, Object> map = new HashMap<String, Object>();
						map.put("id", moduleVO.getId());
						map.put("name", moduleVO.getName());		
						rs.add(map);
						break;
					}
				}
		}
		return rs;
	}
	
	public ModuleVO getModule(Long id){
		ModuleVO module = null;
		ModuleVO item = null;
		List<ModuleVO> moduleList = getModuleList();
		for (ModuleVO m: moduleList) {
			if (m.getId().equals(id)) {
				module = m;
			}
			if (m.getMasterObjId().equals(id)) {
				item = m;
			}
		}
		module.setItem(item);
		return module;
	}
	
	public ModuleVO getModule(String objName ){
		ModuleVO module = null;
		List<ModuleVO> moduleList = getModuleList();
		for (ModuleVO m: moduleList) {
			if (m.getApiName().equals(objName)) {
				module = m;
			}
		}
		return module;
	}
	
	public Integer getTotalNumForFieldType(ModuleVO moduleVO,String mysqlDatatype) {
		Integer mainTotalNum = 0;
		if (mysqlDatatype.equals("int")) {
			mainTotalNum = moduleVO.getMainCustomIntNum();
		} else if (mysqlDatatype.equals("char")) {
			mainTotalNum = moduleVO.getMainCustomCharNum();
		} else if (mysqlDatatype.equals("decimal")) {
			mainTotalNum = moduleVO.getMainCustomDatetimeNum();
		} else if (mysqlDatatype.equals("datetime")) {
			mainTotalNum = moduleVO.getMainCustomDatetimeNum();
		} else if (mysqlDatatype.equals("text")) {
			mainTotalNum = moduleVO.getMainCustomTextNum();
		} else if (mysqlDatatype.equals("blob")) {
			mainTotalNum = moduleVO.getMainCustomBlobNum();
		}
		return mainTotalNum;
	}
	
	public ModuleVO getItemModule(Long id) {
		ModuleVO moduleVO = getModule(id);
		return moduleVO.getItem();
	}
	
	public Map<String, Integer> getTotalNumForMysqlDataTypes(Long objId) {
		ModuleVO moduleVO = getModule(objId);
		Map<String, Integer> rsMap = new HashMap<String, Integer>();
		rsMap.put("int", moduleVO.getMainCustomIntNum());
		rsMap.put("char", moduleVO.getMainCustomCharNum());
		rsMap.put("decimal", moduleVO.getMainCustomDatetimeNum());
		rsMap.put("datetime",moduleVO.getMainCustomDatetimeNum());
		rsMap.put("text",moduleVO.getMainCustomTextNum());
		rsMap.put("blob",moduleVO.getMainCustomBlobNum());
		return rsMap;
	}
}



